// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vant from 'vant';
import 'vant/lib/index.css';
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
import store from './store'
import md5 from 'js-md5';
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
import VueCookies from 'vue-cookies'
import Mui from 'vue-awesome-mui';
Vue.use(Mui);
Vue.use(VueCookies)
Vue.use(MintUI)

Vue.use(Vant);
Vue.config.productionTip = false
// 执行移动端适配
require('./utils/adapter')(document, window);

Vue.prototype.$md5 = md5;
/* 在 0.2.041 之前的版本需要引入 css */
// import 'jwchat/lib/JwChat.css';
Vue.prototype.$axios = axios
Vue.use(ElementUI)
Vue.config.productionTip = false
// 设置反向代理，前端请求默认发送到 http://localhost:8888/api
axios.defaults.baseURL = 'http://localhost:8080'

Vue.config.debug = true;
//钩子函数，访问路由前调用
router.beforeEach((to, from, next) => {
    //路由需要认证
    if (to.meta.requireAuth) {
      //判断store里是否有token
      if (store.state.token) {
        next()
      } else {
        next({
          path: 'login',
          query: {  }
        })
      }
    } else {
      next()
    }
  }
)



// main.js
function formatDate(date, fmt) {
  if(date==null) return
  date = new Date(date);
  if (typeof(fmt) === "undefined") {
    fmt = "yyyy-MM-dd HH:mm:ss";
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'H+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : ('00' + str).substr(str.length));
    }
  }
  return fmt
};

//定义一个时间过滤器
Vue.filter("FormatDate", function(date, fmt) {
  return formatDate(date, fmt);
});
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
